'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/pages/files_mzgl/page_binding/kh/add_aidbind 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			schema_bind_type,
			album_id,
			tv_name,
			_id,
			// episode_id,
			path,
			// key,
			// value,
			app_access_token,
			app_id
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------


		console.log("data=======", data);


		let urlA = "https://open.douyin.com/oauth/client_token/"
		const accessToken = await uniCloud.httpclient.request(urlA, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
			},
			data: {
				client_key: app_id, // id
				client_secret: app_access_token, // 密钥
				grant_type: "client_credential",
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})


		// 有效时间为 2 个小时
		let access_token = accessToken.data.data.access_token

		console.log("access_token===", access_token);


		// 去数据库匹配
		let A = await vk.baseDao.selects({
			dbName: "dy_tv_series",
			pageIndex: 1,
			pageSize: -1,
			// 主表where条件
			whereJson: {
				tv_id: _id
			},
			fieldJson: {
				episode_id: true,
				seq: true,
				tv_id: true
			},
		});

		console.log("A===", A.rows);

		const arr = A.rows
		if (!arr[0].episode_id) {
			return {
				code: 1,
				msg: "剧集id为空，请编辑短剧"
			}
		}
		for (let i = 0; i < arr.length; i++) {
			const tv_id = arr[i].tv_id
			const series = String(arr[i].seq)
			console.log("series====", series);
			const episode_id = arr[i].episode_id


			let urlT = "https://open.douyin.com/api/playlet/v2/album/bind/"
			let resA = await uniCloud.httpclient.request(urlT, {
				method: 'POST',
				headers: {
					"Content-Type": "application/json",
					"access-token": access_token,
				},
				data: {
					schema_bind_type: 1,
					single_schema_bind: {
						album_id: album_id,
						episode_id: episode_id,
						path: path,
						params: [{
								key: "path",
								value: "player"
							},
							{
								key: "tv_id",
								value: tv_id
							},
							{
								key: "series",
								value: series
							},
						]
					}
				},
				dataType: 'json',

			})

			console.log("resA====", resA);
			if (resA.data.err_msg == '应用未获得该能力') {
				return {
					code: 1,
					msg: resA.data.err_msg
				};
			}
			if (resA.data.err_no == 0) {
				// 信息存入数据库
				let id = await vk.baseDao.add({
					dbName: "dy_ymbd",
					dataJson: {
						schema_bind_type: 1,
						app_id: app_id,
						album_id: album_id,
						episode_id: episode_id,
						path: path,
						key1: "tv_id",
						value1: tv_id,
						key2: "series",
						value2: series,
						tv_name: tv_name,
						_add_time_str: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
					}
				})
			}




		}















		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}